package com.changgou.goods.controller;

import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result<Brand> findAll() {
        int i = 1 / 0;
        List<Brand> brandList = brandService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable Integer id) {
        Brand brand = brandService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", brand);
    }

    @PostMapping
    public Result<Object> add(@RequestBody Brand brand) {
        brandService.add(brand);
        return Result.success("添加成功！");
    }

    @PutMapping(value = "/{id}")
    public Result<Object> update(@RequestBody Brand brand, @PathVariable Integer id) {
        brand.setId(id);
        brandService.update(brand);
        return Result.success("更新成功！");
    }

    @DeleteMapping(value = "/{id}")
    public Result<Object> delete(@PathVariable Integer id) {
        brandService.delete(id);
        return Result.success("删除成功！");
    }

    @PostMapping(value = "/search")
    public Result<List<Brand>> findList(@RequestBody(required = false) Brand brand) {
        List<Brand> brandList = brandService.findList(brand);
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable Integer page, @PathVariable Integer size) {
        PageInfo<Brand> info = brandService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "分页成功", info);
    }

    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@RequestBody(required = false) Brand brand, @PathVariable int page, @PathVariable int size) {
        //执行搜索
        PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
        return new Result<>(true, StatusCode.OK, "搜索分页成功", pageInfo);
    }

    @GetMapping("/category/{category}")
    public Result findListByCategoryName(@PathVariable String category) {
        List<Map<String, String>> brandList = brandService.findListByCategoryName(category);
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping(value = "/category/{id}")
    public Result<List<Brand>> findBrandByCategory(@PathVariable(value = "id") Integer categoryId) {
        //调用Service查询品牌数据
        List<Brand> categoryList = brandService.findByCategory(categoryId);
        return new Result<>(true, StatusCode.OK, "查询成功！", categoryList);
    }
}
